#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class SkillRcdProvider : CY.GFive.Core.Providers.DALProvider.ISkillRcdProvider
	{
		#region Sql
		private static readonly string SqlSelectSkillRcd = "SELECT [Id], [CertCode],[StaffCode],[DeclareDate],[KonwLegeLvl],[SkillScore],[Des],[ParentCode] FROM [SkillRcd] WHERE  [Id]=@Id";

		private static readonly string SqlInsertSkillRcd = "INSERT INTO [SkillRcd]([CertCode],[StaffCode],[DeclareDate],[KonwLegeLvl],[SkillScore],[Des],[ParentCode]) values(@CertCode,@StaffCode,@DeclareDate,@KonwLegeLvl,@SkillScore,@Des,@ParentCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateSkillRcd = "UPDATE [SkillRcd] SET [CertCode] = @CertCode,[StaffCode] = @StaffCode,[DeclareDate] = @DeclareDate,[KonwLegeLvl] = @KonwLegeLvl,[SkillScore] = @SkillScore,[Des] = @Des,[ParentCode] = @ParentCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteSkillRcd = "DELETE [SkillRcd] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllSkillRcd = "SELECT [Id], [CertCode],[StaffCode],[DeclareDate],[KonwLegeLvl],[SkillScore],[Des],[ParentCode] FROM [SkillRcd]";
		#endregion

		#region ISkillRcdProvider Members
		public Core.Business.SkillRcd Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectSkillRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.SkillRcd skillRcd = new Core.Business.SkillRcd();

				if (!reader.IsDBNull(0)) skillRcd.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) skillRcd.CertCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) skillRcd.StaffCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) skillRcd.DeclareDate = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) skillRcd.KonwLegeLvl = reader.GetDecimal(4);
				if (!reader.IsDBNull(5)) skillRcd.SkillScore = reader.GetDecimal(5);
				if (!reader.IsDBNull(6)) skillRcd.Des = reader.GetString(6);
				if (!reader.IsDBNull(7)) skillRcd.ParentCode = reader.GetString(7);

				return skillRcd;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.SkillRcd skillRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@CertCode", SqlDbType.NVarChar, skillRcd.CertCode);
			sql.AddParameter("@StaffCode", SqlDbType.NVarChar, skillRcd.StaffCode);
			sql.AddParameter("@DeclareDate", SqlDbType.DateTime, skillRcd.DeclareDate);
			sql.AddParameter("@KonwLegeLvl", SqlDbType.Decimal, skillRcd.KonwLegeLvl);
			sql.AddParameter("@SkillScore", SqlDbType.Decimal, skillRcd.SkillScore);
			sql.AddParameter("@Des", SqlDbType.NVarChar, skillRcd.Des);
			sql.AddParameter("@ParentCode", SqlDbType.NVarChar, skillRcd.ParentCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertSkillRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) skillRcd.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.SkillRcd skillRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@CertCode", SqlDbType.NVarChar, skillRcd.CertCode);
			sql.AddParameter("@StaffCode", SqlDbType.NVarChar, skillRcd.StaffCode);
			sql.AddParameter("@DeclareDate", SqlDbType.DateTime, skillRcd.DeclareDate);
			sql.AddParameter("@KonwLegeLvl", SqlDbType.Decimal, skillRcd.KonwLegeLvl);
			sql.AddParameter("@SkillScore", SqlDbType.Decimal, skillRcd.SkillScore);
			sql.AddParameter("@Des", SqlDbType.NVarChar, skillRcd.Des);
			sql.AddParameter("@ParentCode", SqlDbType.NVarChar, skillRcd.ParentCode);
			sql.AddParameter("@Id", SqlDbType.Int, skillRcd.Id);
			sql.ExecuteSql(SqlUpdateSkillRcd);
		}

		public void Delete(Core.Business.SkillRcd skillRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, skillRcd.Id);
			sql.ExecuteSql(SqlDeleteSkillRcd);
		}

		public IList<Core.Business.SkillRcd> GetAllSkillRcd()
		{
			IList<Core.Business.SkillRcd> skillRcdlist = new List<Core.Business.SkillRcd>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllSkillRcd);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.SkillRcd skillRcd = new Core.Business.SkillRcd();

					if (!reader.IsDBNull(0)) skillRcd.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) skillRcd.CertCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) skillRcd.StaffCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) skillRcd.DeclareDate = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) skillRcd.KonwLegeLvl = reader.GetDecimal(4);
					if (!reader.IsDBNull(5)) skillRcd.SkillScore = reader.GetDecimal(5);
					if (!reader.IsDBNull(6)) skillRcd.Des = reader.GetString(6);
					if (!reader.IsDBNull(7)) skillRcd.ParentCode = reader.GetString(7);

					skillRcd.MarkOld();
					skillRcdlist.Add(skillRcd);
				}
				reader.Close();
			}
			return skillRcdlist;
		}
		#endregion
	}
}
